﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using P2.Cuberry.Framework.Helper;
using System.Collections.ObjectModel;
using P2.Cuberry.Framework.Helper.Configuration;
using P2.Cuberry.Modules.SimpleAccounting.FiCheque.DAL;
using P2.Cuberry.Framework.BOLBase;

namespace P2.Cuberry.Modules.SimpleAccounting.FiCheque.BusinessEntities
{
    class FiChequeToFiTransactionItem : BizObject, INotifyPropertyChanged
    {  
        
        #region TemporaryStorage
        public static List<FiChequeToFiTransactionItem> TemporaryStorage = new List<FiChequeToFiTransactionItem>();
        #endregion


        #region Members

        //++KeyID
        int _fiChequeID = -1;
        public int FiChequeID
        {
            get { return _fiChequeID; }
            protected set
            {
                if (_fiChequeID != value)
                {
                    _fiChequeID = value;
                }
            }
        }

        //Add++
        int _fiscalYearID = -1;
        public int FiscalYearID
        {
            get { return _fiscalYearID; }
            set
            {
                if (value != _fiscalYearID)
                {
                    _fiscalYearID = value;
                    Modified = true;
                }
            }
        }

        int _FiTransactionItemID = 0;
        public int FiTransactionItemID
        {
            get { return _FiTransactionItemID; }
            set
            {
                if (value != _FiTransactionItemID)
                {
                    _FiTransactionItemID = value;
                    Modified = true;
                }
            }
        }


        DateTime?  _CreatedOn = DateTime.Now;
        public DateTime?  CreatedOn
        {
            get { return _CreatedOn; }
            set
            {
                if (value != _CreatedOn)
                {
                    _CreatedOn = value;
                    Modified = true;
                }
            }
        }
      DateTime?  _EditedOn = DateTime.Now;
        public DateTime?  EditedOn
        {
            get { return _EditedOn; }
            set
            {
                if (value != _EditedOn)
                {
                    _EditedOn = value;
                    Modified = true;
                }
            }
        }
         string  _EditedBy = "";
         public string EditedBy
        {
            get { return _EditedBy; }
            set
            {
                if (value != _EditedBy)
                {
                    _EditedBy = value;
                    Modified = true;
                }
            }
        }
         string  _CreatedBy = "";
        public string  CreatedBy
        {
            get { return _CreatedBy; }
            set
            {
                if (value != _CreatedBy)
                {
                    _CreatedBy = value;
                    Modified = true;
                }
            }
        }   

        public FiAccount.BusinessEntities.FiFiscalYear FiscalYear
        {
            get
            {
                return FiAccount.BusinessEntities.FiFiscalYear.GetItem(this.FiscalYearID);
            }
        }

        #endregion


      


        #region Constructors

        public FiChequeToFiTransactionItem()
        {
            Modified = false;
        }


        public FiChequeToFiTransactionItem(int keyFiChequeID)
        {
            this._fiChequeID = keyFiChequeID;
           // FillMyObject();
            Modified = false;
        }


        public FiChequeToFiTransactionItem(DAL.FiCheque fiCheque)
        {
           // Load(fiCheque);
        }

        #endregion


        #region Read Operations

        //load an existing Instance
        //public void Load(int keyFiChequeID)
        //{
        //    FiChequeID = keyFiChequeID;
        //    if (keyFiChequeID >= 0)
        //    {
        //        FillMyObject();
        //    }
        //    else
        //    {
        //        Load(null);                
        //    }
        //    Modified = false;
        //}

        public FiChequeToFiTransactionItem(DAL.FiChequeToFiTransactionItem transactionItem)
        {
            Load(transactionItem);
        }
        //Add++ //DDL++
        public void Load(DAL.FiChequeToFiTransactionItem  fiCheque)
        {
            if(fiCheque != null)
            {
                this.FiChequeID = fiCheque.FiChequeID;
                
                this.FiscalYearID = fiCheque.FiscalYearID;
           
                this.FiTransactionItemID  =fiCheque.FiTransactionItemID;
                
                      
                
                //Default Fields                
                this.CreatedBy = fiCheque.CreatedBy;
                this.CreatedOn = fiCheque.CreatedOn;
                this.EditedBy = fiCheque.EditedBy;
                this.EditedOn = fiCheque.EditedOn;
                this.Timestamp = fiCheque.TStamp;
            }
            else
            {
                this.FiChequeID = -1;

                this.FiscalYearID = -1;
                this.FiChequeID = -1;
                this.FiTransactionItemID = -1;



                //Default Fields               
              
                this.CreatedOn = null;
               
                this.EditedOn = null;
                this.Timestamp = null;
                //Default Fields
                this.CreatedBy = String.Empty;
                this.EditedBy = String.Empty;
               // P2.Cuberry.Framework.Helper.Workflows.WorkflowStatus initStatus = P2.Cuberry.Framework.Helper.Workflows.WorkflowStatus.GetInitialStatus(this.EntityName);
                //this.StatusID = initStatus.StatusID;
            }
            Modified = false;
        }
        public static ObservableCollection<FiChequeToFiTransactionItem> GetTransactiontemsForHeader(FiChequeToFiTransactionItem FiChequeToFiTransactionItems, PropertyChangedEventHandler onChange)
        {
            try
            {
                ObservableCollection<FiChequeToFiTransactionItem> itemsCollection = new ObservableCollection<FiChequeToFiTransactionItem>();

            
                string connectionString = ConfigurationSetUp.GetEntConnectionString("Business", Properties.Settings.Default.EntityResources);
                using (FiChequeDataContext db = new FiChequeDataContext(connectionString))
                {
                    var invItems = from invItem in db.FiChequeToFiTransactionItem
                                   where invItem.FiChequeID == FiChequeToFiTransactionItems.FiChequeID
                                   select invItem;
                    if (invItems.Count() > 0)
                    {
                        foreach (var xinvItem in invItems)
                        {
                            itemsCollection.Add(new FiChequeToFiTransactionItem(xinvItem));
                        }
                    }
                }

                return itemsCollection;
            }
            catch (System.Exception ex) //AM:TC
            {
                P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", "SCSupplierInvoiceItem", System.Reflection.MethodInfo.GetCurrentMethod().Name));
                return null;
            }
        }

        #endregion

        

     


        #region "Copy operations"

        //public SCSupplierInvoiceItem GetCopy()
        //{
        //    try
        //    {
        //        SCSupplierInvoiceItem newItem = new SCSupplierInvoiceItem();
        //        newItem.SCSupplierInvoiceHeaderID = this.SCSupplierInvoiceHeaderID;
        //        newItem.SCSupplierInvoiceItemID = this.SCSupplierInvoiceItemID;
        //        newItem.FiscalYearID = this.FiscalYearID;
        //        newItem.ExpiryDate = this.ExpiryDate;
        //        newItem.ParentSCSupplierInvoice = this.ParentSCSupplierInvoice;
        //        newItem.ProductID = this.ProductID;
        //        // # Added By WLI : 23 March 2012
        //        newItem.VATPercent = this.VATPercent;
        //        //
        //        newItem.PricePerUnit = this.PricePerUnit;
        //        newItem.Quantity = this.Quantity;
        //        // # Added By WLI : 23 March 2012
        //        newItem.Position = this.Position;
        //        //            
        //        newItem.Timestamp = this.Timestamp;
        //        newItem.Modified = this.Modified;
        //        return newItem;
        //    }
        //    catch (System.Exception ex) //AM:TC
        //    {
        //        P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodInfo.GetCurrentMethod().Name));
        //        return null;
        //    }
        //}

        //internal P2TransactionMessage FillMyObject()
        //{
        //    P2TransactionMessage tMsg = new P2TransactionMessage();            
        //    if (this._fiChequeID < 0)
        //    {
        //        Load(this._fiChequeID);
        //        return tMsg;
        //    }

        //    try
        //    {
        //        string connectionString = ConfigurationSetUp.GetEntConnectionString("Business", Properties.Settings.Default.EntityResources);
        //        using (FiChequeDataContext db = new FiChequeDataContext(connectionString))
        //        {
        //            var xElements = from xElement in db.FiCheque
        //                            where xElement.FiChequeID == this._fiChequeID
        //                            select xElement;

        //            foreach (var xElementx in xElements)
        //            {
        //                Load(xElementx);
        //            }
        //        }

        //        tMsg.TransactionStatus = "green";
        //        tMsg.MessageText = "TMsg_FillMyObjectSuceeded";
        //    }
        //    catch (Exception ex)
        //    {
        //        tMsg.TransactionStatus = "red";
        //        tMsg.MessageText = "Error_Error";
        //        tMsg.MessageFreeText = ex.Message;
        //        P2ExceptionHandler.LogException(ex, this);
        //    }            

        //    this.MessageStack.Add(tMsg);
        //    return tMsg;

        //}
        #endregion


       
        #region Property Changed

        public event PropertyChangedEventHandler PropertyChanged;


        private void OnPropertyChanged(string propertyName) 
        {
            Modified = true;
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }

        //Add++ //DDL++
        public void OnAllPropertiesChanged() 
        {
            OnPropertyChanged("FiChequeID");

        OnPropertyChanged("FiscalYearID"); 
        OnPropertyChanged("ChequeNo"); 
        OnPropertyChanged("Bank");
        OnPropertyChanged("MyBank"); 
        OnPropertyChanged("ValueDate"); 
        OnPropertyChanged("Amount"); 
        OnPropertyChanged("FiTransactionID"); 
        OnPropertyChanged("Note"); 


        OnPropertyChanged("CurrencyID"); 
            
                                 
            //Standard Fields
            OnPropertyChanged("StatusName");
            OnPropertyChanged("StatusID");
        }

        #endregion


        #region Validator

        public override P2Validator Validator
        {
            get
            {
                if (_validator == null)
                    _validator = new P2Validator(Properties.Settings.Default.ModuleName, this.EntityName, "FiChequeID", this);
                return _validator;
            }
        }

        #endregion
    }
}
